﻿using System;
using System.Collections;
using System.Collections.Generic;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;

using Meqs.Entity;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    [Transactional]
    public class SchoolInfoLogic : CommonLogic<SchoolInfo, int>, ISchoolInfoLogic
    {
        private IGlobalTermInfoLogic _termLogic;
        private ITermInfoLogic _terminfoLogic;

        public SchoolInfoLogic(IGlobalTermInfoLogic termLogic, ITermInfoLogic terminfoLogic)
        {
            _termLogic = termLogic;
            _terminfoLogic = terminfoLogic;
        }

        #region Implementation of ICommonLogic<SchoolInfo, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(SchoolInfo entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(SchoolInfo entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public SchoolInfo FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<SchoolInfo> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<SchoolInfo> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 查询学校及关联信息
        /// </summary>
        /// <param name="causeID"></param>
        /// <returns></returns>
        public IList<VSchoolInfo> FindVSchoolInfo()
        {
            return FindCustomer<VSchoolInfo>("SchoolInfo.FindVSchoolInfo", null);
        }

        /// <summary>
        /// 查询学校及关联信息
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public IList<VSchoolInfo> FindVSchoolInfo(Hashtable ht)
        {
            return FindCustomer<VSchoolInfo>("SchoolInfo.FindVSchoolInfo", ht);
        }

        /// <summary>
        /// 学年升级
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int YearUpgrade(Hashtable ht)
        {
            InsertCustomer("SchoolInfo.YearUpgrade", ht);

            return 1;
        }

        ///// <summary>
        ///// 获取定性分析的学校
        ///// </summary>
        ///// <returns></returns>
        //public IList<SchoolInfo> SelectGradeKindSchool()
        //{
        //    return FindCustomer<SchoolInfo>("SchoolInfo.SelectGradeKindSchool", null);
        //}


        ///// <summary>
        ///// 获取成绩分布定义的学校
        ///// </summary>
        ///// <returns></returns>
        //public IList<SchoolInfo> SelectGradeDistributionSchool()
        //{
        //    return FindCustomer<SchoolInfo>("SchoolInfo.SelectGradeDistributionSchool", null);
        //}


        ///// <summary>
        ///// 获取分数段的学校
        ///// </summary>
        ///// <returns></returns>
        //public IList<SchoolInfo> SelectGradeSectionsettingsSchool()
        //{
        //    return FindCustomer<SchoolInfo>("SchoolInfo.SelectGradeSectionsettingsSchool", null);
        //}


        ///// <summary>
        ///// 获取统考分数线的学校
        ///// </summary>
        ///// <returns></returns>
        //public IList<SchoolInfo> SelectScorepointGradeSchool()
        //{
        //    return FindCustomer<SchoolInfo>("SchoolInfo.SelectScorepointGradeSchool", null);
        //}

        /// <summary>
        /// 获取局级考试对应的学校
        /// </summary>
        /// <returns></returns>
        public IList<SchoolInfo> SelectSchoolByExam(int examId, int areaid)
        {
            Hashtable ht = new Hashtable();
            ht.Add("examId", examId);
            if (areaid>0)
            {
                ht.Add("AreaID", areaid);
            }
            
            return FindCustomer<SchoolInfo>("SchoolInfo.SelectSchoolByExam", ht);
        }

        /// <summary>
        /// 验证学校信息是否已设置
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public bool CheckSchoolInfoInitState(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            SchoolInfo sch = FindCustomerByID<SchoolInfo>("SchoolInfo.Find", ht);
            if (sch == null) return false;
            return !string.IsNullOrEmpty(sch.SchoolName);
        }

        /// <summary>
        /// 更新学校信息，插入学年学期信息
        /// </summary>
        /// <param name="schoolName"></param>
        /// <param name="year"></param>
        /// <param name="term"></param>
        /// <param name="opt"></param>
        [Transaction(TransactionMode.Supported)]
        public void UpdateSchool(string schoolName, int year, int term, int opt, String schoolMark, String startDate, String endDate)
        {

            SchoolInfo sch = new SchoolInfo(1, "School", "School", 1, schoolName,
                                            (short)opt, year, (byte)term, true);
            Update(sch);
            _termLogic.DeleteAll();
            GlobalTermInfo termInfo = new GlobalTermInfo();
            
            termInfo.GlobalYear = year;
            termInfo.GlobalTerm = term;
            _termLogic.Create(termInfo);

            TermInfo ti = new TermInfo();


            DateTime dts = DateTime.MinValue;
            DateTime dte = DateTime.MinValue;
            if (!DateTime.TryParse(startDate, out dts))
                return;
            ti.StartDate = dts;
            if (DateTime.TryParse(endDate, out dte))
                ti.EndDate = dte;

            ti.SchoolMark = schoolMark;
            ti.SchoolYear = year;
            ti.Term = term;

            _terminfoLogic.Create(ti);
        }
        /// <summary>
        /// 获取学校信息
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public SchoolInfo FindBySchoolMark(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            SchoolInfo sch = FindCustomerByID<SchoolInfo>("SchoolInfo.Find", ht);
            return sch;
        }
    }
}